package org.merak.core.persistence.jdbc;

import java.sql.SQLException;

import org.merak.core.Application;

public abstract class SQLExecutor<T> {

	//~Methods ////////////////////////////////////////////////////////////////////
	//****************************************************************************
	protected abstract T retryOnClosedConnetion() throws SQLException ;

	//****************************************************************************
	public T execute(XConnection conn,SQLException firstError)
		throws SQLException
	{
		/* If connection is closed, try recovering and, if successful,
		 * execute the SQL CallBack again. */
		if (conn.isClosed()) {
			Application.database.getLogger().warn(
					conn.getSettings() + ": Connection lost."
			);
			return this.retryOnClosedConnetion();
		}

		/* Throw the original exception if the problem is unrelated to
		 * closed connection */
		throw firstError;

	}
	//****************************************************************************
}
